package com.admin.testproject.activity

import android.animation.AnimatorInflater
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.animation.*
import com.admin.testproject.BaseActivity
import com.admin.testproject.R
import kotlinx.android.synthetic.main.activity_anim.*

/**
 * author 吴昶
 * date 2019/5/31
 * 标注
 */
class AnimActivity :BaseActivity(){
    override fun bindLayout(): Int {
        return R.layout.activity_anim
    }

    override fun bindData() {
        val drawable=iv_anim.background as AnimationDrawable
        drawable.isOneShot=false

        iv_anim.setOnClickListener {
            if(isPlaying){
                drawable.stop()
            }else{
                drawable.start()
            }
            isPlaying=!isPlaying
        }

        iv_alpha.setOnClickListener {
            start()
        }

        iv_object.setOnClickListener {
            val an=AnimatorInflater.loadAnimator(this, R.animator.`object`)
            an.setTarget(iv_object)
            an.start()
        }
    }

    override fun bindEvent() {

    }

    var isPlaying=false

    fun start(){
        Log.d("******","++++++++++++")
        val set=AnimationSet(true)
        set.addAnimation(alpha())
        set.addAnimation(scale())
//        set.addAnimation(trans())
        set.addAnimation(rotate())
        iv_alpha.startAnimation(set)
    }

    //透明度
    fun alpha():AlphaAnimation{
        val alpha=AlphaAnimation(0f,1.0f)
        alpha.duration=1000
        alpha.fillAfter=false
//        iv_alpha.animation=alpha
        return alpha
    }
//缩放
    fun scale():ScaleAnimation{
        val scale=ScaleAnimation(1f,4f,1f,4f,
            Animation.RELATIVE_TO_SELF,0.5f,
            Animation.RELATIVE_TO_SELF,0.5f)
        scale.duration=2000
        scale.fillAfter=false
//        iv_alpha.animation=scale
        return scale
    }
//位移
    fun trans():TranslateAnimation{
        val translateAnimation=TranslateAnimation(
            Animation.RELATIVE_TO_SELF,0f,
            Animation.RELATIVE_TO_SELF,2f,
            Animation.RELATIVE_TO_SELF,0f,
            Animation.RELATIVE_TO_SELF,2f
        )
        translateAnimation.duration=3000
        translateAnimation.fillAfter=false
        return translateAnimation
    }

    //旋转
    fun rotate():RotateAnimation{
        val rotate=RotateAnimation(
            0f,-720f,
            Animation.RELATIVE_TO_SELF,0.5f,
            Animation.RELATIVE_TO_SELF,0.5f
        )
        rotate.duration=4000
        rotate.fillAfter=false
        return rotate
    }

}